package com.wangjiang.mario.admin.common.util;

import com.wangjiang.mario.admin.common.constant.MdcConstant;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * log 工具类 (线程追踪)
 * @author  wangjiang
 */
public class LogUtil {
    /**
     * 生成日志唯一追踪id
     * @return
     */
    public static String genTraceID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 开始线程日志追踪
     */
    public static void startTraceRandom(String uuid) {
        if(CommonUtils.isNull(uuid)) {
            uuid = genTraceID();
        }
        MDC.put(MdcConstant.TRACE_ID , uuid);
    }

    public static String getTraceId() {
        return MDC.get(MdcConstant.TRACE_ID);
    }

    /**
     * 删除日志跟踪
     */
    public static void removeTrace() {
        MDC.remove(MdcConstant.TRACE_ID);
    }
}
